package com.xxl.baselib.util;


import com.xxl.baselib.app.AppConfig;

import java.util.Hashtable;


/**
 * Title: LogUtil.java
 * Description:
 * Copyright (c) 版权所有请勿抄袭 2018
 * Created DateTime: 2018/4/9 15:07
 * Created by xuelianx.
 */
public class LogUtil {

    public final static String tag = "[APPName]"; //APP名称
    private final static int logLevel = android.util.Log.VERBOSE;
    private static Hashtable<String, LogUtil> sLoggerTable = new Hashtable<String, LogUtil>();
    private String mClassName;

    private static LogUtil xxlLog;

    private static final String XXL = "@xxl@ ";


    private LogUtil(String name) {
        mClassName = name;
    }

    /**
     * Purpose:XXL user one
     *
     * @return LogUtil
     */
    public static LogUtil xxlLog() {
        if (xxlLog == null) {
            xxlLog = new LogUtil(XXL);
        }
        return xxlLog;
    }

    /**
     * Get The Current Function Name
     *
     * @return String
     */
    private String getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();
        if (sts == null) {
            return null;
        }
        for (StackTraceElement st : sts) {
            if (st.isNativeMethod()) {
                continue;
            }
            if (st.getClassName().equals(Thread.class.getName())) {
                continue;
            }
            if (st.getClassName().equals(this.getClass().getName())) {
                continue;
            }
            return mClassName + "[ " + Thread.currentThread().getName() + ": "
                    + st.getFileName() + ":" + st.getLineNumber() + " "
                    + st.getMethodName() + " ]";
        }
        return null;
    }

    /**
     * The LogUtil Level:i
     *
     * @param str
     */
    public void i(Object str) {
        if (AppConfig.isLogEnabled) {
            if (logLevel <= android.util.Log.INFO) {
                String name = getFunctionName();
                if (name != null) {
                    android.util.Log.i(tag, name + " - " + str);
                } else {
                    android.util.Log.i(tag, str.toString());
                }
            }
        }

    }

    /**
     * The LogUtil Level:d
     *
     * @param str
     */
    public void d(Object str) {
        if (AppConfig.isLogEnabled) {
            String name = getFunctionName();
            if (name != null) {
                android.util.Log.d(tag, name + " - " + str);
            } else {
                android.util.Log.d(tag, str.toString());
            }

        }
    }

    /**
     * The LogUtil Level:V
     *
     * @param str
     */
    public void v(Object str) {
        if (AppConfig.isLogEnabled) {
            if (logLevel <= android.util.Log.VERBOSE) {
                String name = getFunctionName();
                if (name != null) {
                    android.util.Log.v(tag, name + " - " + str);
                } else {
                    android.util.Log.v(tag, str.toString());
                }
            }
        }
    }

    /**
     * The LogUtil Level:w
     *
     * @param str
     */
    public void w(Object str) {
        if (AppConfig.isLogEnabled) {
            if (logLevel <= android.util.Log.WARN) {
                String name = getFunctionName();
                if (name != null) {
                    android.util.Log.w(tag, name + " - " + str);
                } else {
                    android.util.Log.w(tag, str.toString());
                }
            }
        }
    }

    /**
     * The LogUtil Level:e
     *
     * @param str
     */
    public void e(Object str) {
        if (AppConfig.isLogEnabled) {
            if (logLevel <= android.util.Log.ERROR) {
                String name = getFunctionName();
                if (name != null) {
                    android.util.Log.e(tag, name + " - " + str);
                } else {
                    android.util.Log.e(tag, str.toString());
                }
            }
        }
    }

    /**
     * The LogUtil Level:e
     *
     * @param ex
     */
    public void e(Exception ex) {
        if (AppConfig.isLogEnabled) {
            if (logLevel <= android.util.Log.ERROR) {
                android.util.Log.e(tag, "error", ex);
            }
        }
    }

    /**
     * The LogUtil Level:e
     *
     * @param log
     * @param tr
     */
    public void e(String log, Throwable tr) {
        if (AppConfig.isLogEnabled) {
            String line = getFunctionName();
            android.util.Log.e(tag, "{Thread:" + Thread.currentThread().getName() + "}"
                    + "[" + mClassName + line + ":] " + log + "\n", tr);
        }
    }
}